import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpServicesProvider } from "../../providers/http-services/http-services";
import { ValidateProvider } from "../../providers/validate/validate";
import { ConfigProvider } from "../../providers/config/config";
import { UtilsProvider } from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/resume/edit',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-person-resume-edit',
    templateUrl: 'person-resume-edit.html',
})
export class PersonResumeEditPage {
    token: string;
    resume = {};
    default = {
        gender: '男',
        edu: '学历',
        job_status: '工作状态',
    };
    options = {};
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public validate: ValidateProvider,
        public config: ConfigProvider,
        public util: UtilsProvider
    ) {
    }

    //判断用户是否登录
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewWillLoad() {
        this.token = this.config.getToken();
        this.getResume();
    }

    getResume() {
        //获取简历信息
        let apiUrl_resume = "/user_info/my_resume_detail/token/" + this.token;
        this.httpService.requestData(apiUrl_resume, response => {
            if (response['error'] === 200) {
                this.resume = response['data'];
                if (this.resume['cuisine']) {
                    this.resume['cuisine'] = this.resume['cuisine'].split(",");
                }
                if (this.resume['target_work']) {
                    this.resume['target_work'] = this.resume['target_work'].split(",");
                }

            } else {
                this.util.presentAlert(response['message']);
            }
        });

        //获取简历选项
        let apiUrl_options = "/user_info/resume_option";
        this.httpService.requestData(apiUrl_options, response => {
            if (response['error'] === 200) {
                this.options = response['data'];
            } else {
                this.util.presentAlert(response['message']);
            }
        });
    }
    selCheckData(item, option) {
        if (!this.util.isArray(this.resume[item])) {
            this.resume[item] = [];
        }

        if (this.util.inArray(this.resume[item], option) < 0) {
            if (item != 'cuisine' && item != 'remark') {
                if (this.resume[item].length > 2) {
                    this.util.presentAlert('最多只能选择三个职位');
                    return
                }
            }

            this.resume[item].push(option);
        } else {
            let index = this.util.inArray(this.resume[item], option);
            if (index !== -1) {
                this.resume[item].splice(index, 1);
            }
        }
    }
    selSex() {
        let options = [{ value: 1, text: '男' }, { value: 2, text: '女' }];
        this.util.presentActionSheet(options, option => {
            this.resume['sex'] = option.value;
            this.default['gender'] = option.text;
        }, '请选择性别');
    }

    selEdu() {
        let options = [];
        for (let option of this.options['edu']) {
            options.push({
                text: option,
                value: option
            })
        }
        this.util.presentActionSheet(options, option => {
            this.resume['edu'] = option.value;
        }, '请选择学历信息');
    }

    selJobStatus() {
        let options = [];
        for (let option of this.options['job_status']) {
            options.push({
                text: option.value,
                value: option.value
            })
        }
        this.util.presentActionSheet(options, option => {
            this.resume['job_status'] = option.value;
            this.default['job_status'] = option.text;
        }, '请选择工作状态');
    }

    addExperience() {
        if (!this.util.isArray(this.resume['experience'])) {
            this.resume['experience'] = [];
        }
        if (this.resume['experience'].length >= 3) {
            this.util.presentAlert('最多可添加3个工作信息');
            return false;
        }
        this.resume['experience'].push({ 'job': '', 'start_time': '', 'end_time': '', 'company': '' });
    }

    uploadImage(e) {
        let file = e.target.files[0];
        let data = new FormData();
        data.append('image', file);
        data.append('type', 'moments');
        data.append('water', 'true');
        this.httpService.uploadImage('/user/upload_file', data, result => {
            if (!this.util.isArray(this.resume['photo'])) {
                this.resume['photo'] = [];
            }
            this.resume['photo'].push(result.img_path);
        });
    }

    delImage() {
        this.util.presentConfirm('确定删除吗？', () => {
            this.resume['photo']="";
        });
    }

    delWork(i) {
        this.util.presentConfirm('确定删除吗？', () => {
            this.resume['experience'].splice(i, 1);
        });
    }

    uploadHeadImg(e) {
        let file = e.target.files[0];
        if (file == undefined) {
            return false;
        }
        let data = new FormData();
        data.append('image', file);
        data.append('type', 'resume');
        data.append('water', 'true');
        this.httpService.uploadImage('/user/upload_file', data, result => {
            if (result.code == 200) {
                this.resume['photo'] = result.img_path;
            } else {
                this.util.presentAlert('头像上传失败，请稍后再试');
            }
        });
    }

    submit() {
        this.resume['experience_json'] = JSON.stringify(this.resume['experience']);
        let apiUrl = "/user_info/edit_resume/token/" + this.token;
        this.httpService.requestPostData(apiUrl, this.resume, response => {
            if (response['error'] === 200) {
                this.util.presentAlert('简历提交成功，后台正在审核');
                this.navCtrl.popToRoot({
                    animate: true,
                    direction: 'forward'
                });
            } else {
                this.util.presentAlert(response['message']);
            }
        });
    }
}
